package com.mcxx.modules.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.constant.MQConstant;
import com.mcxx.modules.system.service.AreaMappingService;
import com.mcxx.modules.base.entity.AreaEntity;
import com.mcxx.modules.base.entity.AreaGroupEntity;
import com.mcxx.modules.base.entity.AreaMappingEntity;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.base.service.write.AreaMappingWriteService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.system.dto.AreaMappingDTO;
import com.mcxx.modules.system.dto.IssuingBankDTO;
import com.mcxx.modules.system.dto.MQMessageDTO;
import com.mcxx.modules.system.entity.IssuingBankEntity;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.param.AreaMappingParam;
import com.mcxx.modules.system.param.IssuingBankParam;
import com.mcxx.modules.system.service.read.IssuingBankReadService;
import com.mcxx.modules.system.service.write.IssuingBankWriteService;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import com.yinhai.ta404.core.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
@TaTransactional
public class AreaMappingServiceImpl extends BaseService implements AreaMappingService {
    @Autowired
    private AreaMappingWriteService areaMappingWriteService;
    @Autowired
    private AreaReadService areaReadService;
    @Autowired
    private IssuingBankReadService issuingBankReadService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;

    @Override
    public void saveMappingInfo(AreaMappingParam areaMappingParam, UserAccountVo userAccountVo) {

//        areaMappingWriteService.deleteAreaMappingByRescueCode(areaMappingParam.getAreaCode());

        List<AreaEntity> areaEntities = areaReadService.getInfoByPid(areaMappingParam.getAreaId());
        for(AreaEntity e:areaEntities){
            areaMappingWriteService.deleteAreaMappingByRescueCode(e.getCode());
        }

        List<String> financeList = areaMappingParam.getFinanceList();
        String areaCode = areaMappingParam.getAreaCode();
        if(financeList.size()>0){
            HashSet h = new HashSet(financeList);
            financeList.clear();
            financeList.addAll(h);
            for(String financeCode:financeList){

                AreaGroupEntity areaGroupEntity = areaReadService.getAreaGroupByFinanceCode(financeCode);

                AreaMappingEntity areaMappingEntity = new AreaMappingEntity();
                areaMappingEntity.setId(UUIDUtils.getUUID());
                areaMappingEntity.setRescueId(areaMappingParam.getAreaId());
                areaMappingEntity.setRescueName(areaMappingParam.getAreaName());
                areaMappingEntity.setRescueCode(areaMappingParam.getAreaCode());
                areaMappingEntity.setFinanceId(areaGroupEntity.getId());
                areaMappingEntity.setFinanceName(areaGroupEntity.getName());
                areaMappingEntity.setFinanceCode(areaGroupEntity.getCode());
                areaMappingWriteService.insetAreaMapping(areaMappingEntity);
                if("1".equals(areaGroupEntity.getLevel())) {
                    areaCode = areaCode.substring(0,9);


                    List<AreaGroupEntity> areaGroupEntities = areaReadService.getAreaGroupByPid(areaGroupEntity.getId());
                    for (AreaGroupEntity areaGroup : areaGroupEntities) {
                        AreaMappingEntity areaMapping = new AreaMappingEntity();

                        areaMapping.setId(UUIDUtils.getUUID());

                        List<AreaEntity> areaEntityList = areaReadService.getInfoByNameAndPid(areaGroup.getName(), areaMappingParam.getAreaId(), "5");
                        if (areaEntityList.size() != 1) {
                            log.info("映射村级{}的组织机构失败", areaGroup.getName());
                            continue;
                        }
                        areaMapping.setRescueId(areaEntityList.get(0).getId());
                        areaMapping.setRescueName(areaEntityList.get(0).getName());
                        areaMapping.setRescueCode(areaEntityList.get(0).getCode());
                        areaMapping.setFinanceId(areaGroup.getId());
                        areaMapping.setFinanceName(areaGroup.getName());
                        areaMapping.setFinanceCode(areaGroup.getCode());
                        areaMappingWriteService.insetAreaMapping(areaMapping);
                    }
                }
            }



        }
    }


    @Override
    public ExcelInfo downLoadAreaMappingList(AreaMappingParam param, UserAccountVo accountVo) {




        List<AreaMappingDTO> list = areaReadService.downLoadAreaMappingList(param,accountVo);


        ExcelInfo excelInfo = exportExcelService.exportOrgOpLogs(AreaMappingDTO.class,list,"行政区划映射信息.xls",
                "行政区划映射信息","行政区划映射信息",false,null);



        return excelInfo;
    }


}
